package com.fy.spring.serivce.impl;

import com.fy.spring.serivce.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

@Service
public class PaymentServiceImpl implements PaymentService {

    //@HystrixCommand:启用hystrix
    @HystrixCommand(fallbackMethod = "serviceDownLevel", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")}) // 超时3s服务降级
    @Override
    public String timeout(int second) {
        try {
            Thread.sleep(second * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "程序运行" + second + "s";
    }
    // 该方法权限修饰符可以是private，可以重载，但是形参必须和注解下的方法一致
    private String serviceDownLevel(int second) {
        return Thread.currentThread().getName() + "-----------serviceDownLevel----timeout----";
    }

    // @HystrixCommand:启用hystrix
    @HystrixCommand(fallbackMethod = "serviceDownLevel", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")}) // 超时3s服务降级
    @Override
    public String makeException() {
        int a = 10 / 0;
        return null;
    }

    private String serviceDownLevel() {
        return Thread.currentThread().getName() + "-----------serviceDownLevel----timeout----";
    }
}
